import { setTheme, getTheme } from '@/global/storage'

const state = {
  pageLoading: false,
  pageLoadingText: '',
  isCollapse: false,
  theme: getTheme() || 'light'
}

const mutations = {
  SET_PAGE_LOADING(state, loading) {
    state.pageLoadingText = loading ? '正在请求，请稍候。。。' : '请求成功'
    state.pageLoading = loading
  },
  SET_PAGE_LOADING_TEXT(state, text) {
    state.pageLoadingText = text
  },
  toggleCollapse(state) {
    state.isCollapse = !state.isCollapse
  },
  toggleTheme(state) {
    const theme = state.theme === 'dark' ? 'light' : 'dark'
    setTheme(theme)
    state.theme = theme
  }
}

const actions = {
  setPageLoading({ commit }, loading) {
    commit('SET_PAGE_LOADING', loading)
  },
  setPageLoadingText({ commit }, text) {
    commit('SET_PAGE_LOADING_TEXT', text)
  },
  toggleCollapse({ commit }) {
    commit('toggleCollapse')
  },
  toggleTheme({ commit }) {
    commit('toggleTheme')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
